﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPFStudy.PrismStudy.ViewModels
{
    public class NavigateViewModel: BindableBase
    {
        private static int _navCount = 0;

        /// <summary>
        /// 区域管理
        /// </summary>
        private readonly IRegionManager _regionManager;

        /// <summary>
        /// 当行日志
        /// </summary>
        private IRegionNavigationJournal _navigationJournal;

        #region 向后

        /// <summary>
        /// 向后 - 命令
        /// </summary>
        private DelegateCommand _backCmd;

        /// <summary>
        /// 向后 - 命令
        /// </summary>
        public DelegateCommand BackCmd =>
            _backCmd ?? (_backCmd = new DelegateCommand(ExecuteBack));

        /// <summary>
        /// 执行 - 向后
        /// </summary>    
        void ExecuteBack()
        {
            if (_navigationJournal?.CanGoBack == true)
            {
                _navigationJournal?.GoBack();
            }
        }

        #endregion 向后

        #region 向前

        /// <summary>
        /// 向前 - 命令
        /// </summary>
        private DelegateCommand _forwardCmd;

        /// <summary>
        /// 向前 - 命令
        /// </summary>
        public DelegateCommand ForwardCmd =>
            _forwardCmd ?? (_forwardCmd = new DelegateCommand(ExecuteForward));

        /// <summary>
        /// 执行 - 向前
        /// </summary>    
        void ExecuteForward()
        {
            if (_navigationJournal?.CanGoForward == true)
            {
                _navigationJournal?.GoForward();
            }
        }

        #endregion 向前

        #region 打开区域

        /// <summary>
        /// 打开区域 - 命令
        /// </summary>
        private DelegateCommand<string> _openRegionCmd;

        /// <summary>
        /// 打开区域 - 命令
        /// </summary>
        public DelegateCommand<string> OpenRegionCmd =>
            _openRegionCmd ?? (_openRegionCmd = new DelegateCommand<string>(ExecuteOpenRegion));

        /// <summary>
        /// 执行 - 打开区域
        /// </summary>    
        void ExecuteOpenRegion(string parameter)
        {
            //首先通过IRegionManager接口获取当前全局定义的可用区域
            //往这个区域动态的去设置内容
            //设置内容的方式是通过依赖注入的形式

            //参数传递
            _navCount++;
            NavigationParameters keys = new NavigationParameters();
            keys.Add("Title", $"Hellow!~ , Button CommandParams = {parameter + _navCount} ; ");

            //_regionManager.Regions["ContentRegionNavigate"].RequestNavigate(parameter,keys);
            _regionManager.Regions["ContentRegionNavigate"].RequestNavigate(parameter,callback =>
            {
                if (callback.Result == true)
                {
                    _navigationJournal = callback.Context.NavigationService.Journal;
                }
            }, keys);

        }

        #endregion 打开区域

        public NavigateViewModel()
        {

        }

        public NavigateViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;
        }
    }
}
